/**
 * 项目导航首页
 */

import { NavLink, Link, Redirect } from 'react-router-dom'
import React, { Component } from 'react';
import './project-home.css';

const cch = {
  path: '/project/cch',
  name: "长城汇"
}

const ggtd1 = {
  path: '/project/ggtd1',
  name: "光谷天地一期"
}

const ggtd2 = {
  path: '/project/ggtd2',
  name: "光谷天地二期"
}
const jwjr = {
  path: '/project/jwjr',
  name: "健吾假日广场"
}
const jxlc = {
  path: '/project/jxlc',
  name: "锦绣龙城"
}

const dsc = {
  path: '/project/dsc',
  name: "达尚城"
}

const dwgtxc = {
  path: '/project/dwgtxc',
  name: "大悟高铁新城"
}

const DataMap = {
  'cch': cch,
  'ggtd1': ggtd1,
  'ggtd2': ggtd2,
  'jwjr': jwjr,
  'jxlc': jxlc,
  'dsc': dsc,
  'dwgtxc': dwgtxc // admin
}




class ProjectHome extends Component {

  constructor(props) {
    super(props);
    this.state = { routes: [], redirect: null };
  }

  componentWillMount() {
    let userInfo = JSON.parse(localStorage.getItem('userInfo'));

    var routes = [];
    for (let i in userInfo) {
      if (userInfo[i] > 0 && DataMap[i]) {
        routes.push(DataMap[i]);
      }
    }

    if (routes.length > 0) {
      this.setState({ routes: routes });
    } else {
      this.setState({ redirect: <Redirect to="/" /> })
    }
  }

  logOut = () => {
    localStorage.clear('token');
    localStorage.clear('userInfo');
  }

  render() {
    const routes = this.state.routes;
    const redirect = this.state.redirect;
    return (
      <div>
        {redirect}
        <div className="page page2 page3">
          <div className="item building_layout-project">
            <ul>
              {routes.map((item, i) => (
                <li key={i}>
                  <NavLink to={item.path} >{item.name}</NavLink>
                </li >
              ))}
            </ul>
            <Link to="/home" className="list-fh"><img src="../../img/list/fh.png" /></Link>
            <Link to="/" className="list-zx" onClick={this.logOut}><img src="../../img/list/zx.png" /></Link>
          </div>
        </div>
      </div>
    )
  }
}


export default ProjectHome;
